function [capacity_fading, outage_probability, diversity_gain, fig_fading] = fading_capacity(EbN0_dB, channel_types, diversity_orders)
% 衰落信道容量分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   channel_types - 信道类型 ('rayleigh', 'rician', 'nakagami')
%   diversity_orders - 分集阶数数组
% 输出参数：
%   capacity_fading - 衰落信道容量
%   outage_probability - 中断概率
%   diversity_gain - 分集增益
%   fig_fading - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
bandwidth = 180e3; % 带宽 (Hz)
noise_power_dBm = -174 + 10*log10(bandwidth);
noise_power = 10^((noise_power_dBm-30)/10);
num_realizations = 1000; % 信道实现次数
outage_threshold = 0.1; % 中断概率阈值

% 初始化结果数组
num_snr = length(EbN0_dB);
num_channel_types = length(channel_types);
num_diversity = length(diversity_orders);
capacity_fading = zeros(num_snr, num_channel_types, num_diversity);
outage_probability = zeros(num_snr, num_channel_types, num_diversity);
diversity_gain = zeros(num_channel_types, num_diversity);

fprintf('衰落信道容量分析...\n');

% 衰落信道容量计算
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for ch_idx = 1:num_channel_types
        channel_type = channel_types{ch_idx};
        
        for div_idx = 1:num_diversity
            L = diversity_orders(div_idx); % 分集阶数
            
            % 生成衰落信道系数
            channel_realizations = zeros(num_realizations, L);
            
            switch lower(channel_type)
                case 'rayleigh'
                    % 瑞利衰落
                    channel_realizations = sqrt(0.5) * (randn(num_realizations, L) + 1i * randn(num_realizations, L));
                    
                case 'rician'
                    % 莱斯衰落 (K因子=5dB)
                    K_dB = 5;
                    K_linear = 10^(K_dB/10);
                    LOS_component = sqrt(K_linear/(K_linear+1));
                    NLOS_component = sqrt(1/(K_linear+1));
                    
                    channel_realizations = LOS_component + ...
                                          NLOS_component * sqrt(0.5) * (randn(num_realizations, L) + 1i * randn(num_realizations, L));
                    
                case 'nakagami'
                    % Nakagami-m衰落 (m=2)
                    m = 2;
                    channel_realizations = sqrt(sum(randn(num_realizations, L, 2*m).^2, 3) / (2*m));
                    
                otherwise
                    % 默认为瑞利衰落
                    channel_realizations = sqrt(0.5) * (randn(num_realizations, L) + 1i * randn(num_realizations, L));
            end
            
            % 计算有效信道增益 (选择合并)
            if L > 1
                % 选择合并 (SC)
                [~, max_idx] = max(abs(channel_realizations), [], 2);
                effective_channel = abs(channel_realizations(sub2ind(size(channel_realizations), (1:num_realizations)', max_idx)));
            else
                effective_channel = abs(channel_realizations(:, 1));
            end
            
            % 计算瞬时容量
            instantaneous_capacity = zeros(num_realizations, 1);
            for realization = 1:num_realizations
                h = effective_channel(realization);
                instantaneous_snr = snr_linear * h^2 / noise_power;
                
                if instantaneous_snr > 0
                    instantaneous_capacity(realization) = bandwidth * log2(1 + instantaneous_snr);
                end
            end
            
            % 平均容量
            capacity_fading(snr_idx, ch_idx, div_idx) = mean(instantaneous_capacity);
            
            % 中断概率 (容量低于阈值的概率)
            outage_probability(snr_idx, ch_idx, div_idx) = ...
                sum(instantaneous_capacity < outage_threshold * capacity_fading(snr_idx, ch_idx, div_idx)) / num_realizations;
        end
    end
end

% 计算分集增益
for ch_idx = 1:num_channel_types
    for div_idx = 2:num_diversity
        % 分集增益定义为容量提升比例
        L = diversity_orders(div_idx);
        L_ref = diversity_orders(1);
        
        % 在中等SNR处计算增益
        snr_mid = round(num_snr/2);
        diversity_gain(ch_idx, div_idx) = 10*log10(capacity_fading(snr_mid, ch_idx, div_idx) / ...
                                                           capacity_fading(snr_mid, ch_idx, 1));
    end
end

%% 可视化结果
fig_fading = figure('Name', '衰落信道容量分析', 'Position', [100, 100, 1200, 800]);

% 子图1: 容量 vs SNR (不同信道类型)
subplot(2, 2, 1);
for ch_idx = 1:num_channel_types
    plot(EbN0_dB, capacity_fading(:, ch_idx, 1)/1e6, 'LineWidth', 2, 'Color', colors(ch_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('衰落信道容量 vs 信噪比 (L=1)');
legend(channel_types, 'Location', 'NorthWest');

% 子图2: 容量 vs SNR (不同分集阶数)
subplot(2, 2, 2);
ch_fixed = 1; % 瑞利衰落
for div_idx = 1:num_diversity
    plot(EbN0_dB, capacity_fading(:, ch_fixed, div_idx)/1e6, 'LineWidth', 2, 'Color', colors(div_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title(sprintf('分集技术对容量的影响 (%s衰落)', channel_types{ch_fixed}));
legend(arrayfun(@(x) sprintf('L=%d', x), diversity_orders, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图3: 中断概率 vs SNR
subplot(2, 2, 3);
for ch_idx = 1:num_channel_types
    plot(EbN0_dB, outage_probability(:, ch_idx, 1), 'LineWidth', 2, 'Color', colors(ch_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('中断概率');
title('衰落信道中断概率 (L=1)');
legend(channel_types, 'Location', 'NorthEast');
set(gca, 'YScale', 'log');

% 子图4: 分集增益
subplot(2, 2, 4);
for ch_idx = 1:num_channel_types
    bar(diversity_orders(2:end), diversity_gain(ch_idx, 2:end), 'FaceColor', colors(ch_idx, :));
    hold on;
end
grid on;
xlabel('分集阶数 L');
ylabel('分集增益 (dB)');
title('分集增益 vs 分集阶数');
legend(channel_types, 'Location', 'NorthWest');

fprintf('衰落容量分析完成！\n');

end